<template>
  <div class="app-container">
    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%;">
      <el-table-column label="单日优惠最大次数限制" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.discountNum }}</span>
        </template>
      </el-table-column>
      <el-table-column label="有订单时超时时间限制(分钟)" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.orderTimeLimit }}</span>
        </template>
      </el-table-column>
      <el-table-column label="有订单时超时时间加时限制(分钟)" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.orderTimeAddLimit }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单在离开前生成时间限制(分钟)" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.beforeOrderTimeLimit }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单在离开后生成时间限制(分钟)" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.afterOrderTimeLimit }}</span>
        </template>
      </el-table-column>
      <el-table-column label="短途最大路程长度限制(公里)" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.orderDistanceLimit }}</span>
        </template>
      </el-table-column>
      <el-table-column label="优先次数重置时间点" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.refreshTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否离线" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.offLine | offLineFilter }}</span>
        </template>
      </el-table-column>
      <el-table-column label="重复扫描间隔秒数" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.reScanLimit }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="type == 0" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="{ row }">
          <el-button size="mini" icon="el-icon-edit" type="primary" @click="handleEdit(row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="getList" />
    <div v-if="dialogEditFormVisible">
      <el-drawer :visible.sync="dialogEditFormVisible" direction="rtl" size="85%" :append-to-body="true" :destroy-on-close="true" :wrapper-closable="false" :with-header="false">
        <SettingAddForm :id="editId" ref="settingEditForm" @cancelClose="cancelClose(arguments)" />
      </el-drawer>
    </div>
  </div>
</template>

<script>
import { getSysSetting } from '@/api/setting';
import waves from '@/directive/waves';
import Pagination from '@/components/Pagination';
import SettingAddForm from '@/views/sys/settingAdd.vue';

var that;

export default {
  name: 'SysSetting',
  components: { Pagination, SettingAddForm },
  directives: { waves },
  filters: {
    offLineFilter(item) {
      const offLineKeyValue = that.offLineOptions.reduce((acc, cur) => {
        acc[cur.key] = cur.label;
        return acc;
      }, {});
      return offLineKeyValue[item];
    }
  },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        pageNum: 1,
        pageSize: 10
      },
      offLineOptions: [{ label: '否', key: 0 }, { label: '是', key: 1 }],
      dialogEditFormVisible: false,
      editId: 0,
      type: null
    };
  },
  created() {
    that = this;
    const { params } = this.$route
    Object.keys(params).forEach(key => {
      this.type = params[key].split(':')[1];
    });
    this.getList();
  },
  methods: {
    async getList() {
      this.listLoading = true;
      await getSysSetting(this.listQuery).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.listLoading = false;
      });
    },
    async handleSearch() {
      this.getList();
    },
    handleEdit(row) {
      this.editId = row.id;
      this.dialogEditFormVisible = true;
    },
    cancelClose(data) {
      if (data[1] === 'edit') {
        this.dialogEditFormVisible = false;
      }
      if (data[0]) {
        this.getList();
      }
    }
  }
};
</script>
